<template>
	<view class="search-box">
		<input type="text" :placeholder="placeholder" :focus="focus" value="" @input="searchInput"
			class="u-search-input" @blur="searchBlur" :maxlength="maxlength" />
		<scroll-view scroll-y="true" style="height: 200px;margin: 0 auto;">
			<view v-for="(item,index) in searchArr" :key="index" class="u-search-list" @click="selectIndex(item)">
				{{ searchKey ? item[searchKey].codeName : item.codeName}}
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		name: 'uSearch',
		computed: {
			searchArr() {
				var that = this;
				var newArr = []
				var len = that.dictArr.length
				if (!that.searchKey) {
					for (var i = 0; i < len; i++) {
						if (that.dictArr[i].codeName.indexOf(that.searchModel) > -1) {
							newArr.push(that.dictArr[i])
						}
					}
				} else {
					for (var i = 0; i < len; i++) {
						if (that.dictArr[i][that.searchKey].codeName.indexOf(that.searchModel) > -1) {
							newArr.push(that.dictArr[i])
						}
					}
				}
				return newArr
			}
		},
		props: {
			"dictArr": {
				type: Array,
				default () {
					return []
				}
			},
			"searchKey": {
				type: String,
				default: ''
			},
			"placeholder": {
				type: String,
				default: ''
			},
		},
		data() {
			return {
				focus: true,
				searchModel: '',
				maxlength: -1
			}
		},
		created() {

		},
		mounted() {

		},
		methods: {
			searchInput(e) {
				this.searchModel = e.detail.value
			},
			searchBlur(e) {
				this.$emit('blurIndexValue', e.detail.value);
			},
			getSearchInputValue() {
				return this.searchModel
			},
			selectIndex(data) {
				this.$emit('selectIndexValue', data);
			}
		}
	}
</script>

<style>
	.u-search-input {
		margin: 20rpx 30rpx 0 30rpx;
		padding: 20rpx 20rpx;
		height: 40rpx;
		border-radius: 24rpx;
		border: 1px solid #CCCCCC;
	}

	.u-search-list {
		margin: 20rpx 30rpx;
		padding: 11px 20rpx;
		border-radius: 10rpx;
		background: #FFFFFF;
		box-shadow: 5px 5px 5px 0px rgba(0, 0, 0, 0.05);
	}
</style>
